/*
 * *全部病人信息查询页面JS.
 * 2017-08-08
 */
// 全局变量
var bce01 = $.cookie("employeeId");
var bce03;
var valueArr = {};// 参数
var loginName = $.cookie("loginName");
var pinquiry = true;
var cbm01;
// 界面初始化完成后加载的脚本
$(document).ready(function() {
	yiyuanId =$("#hospital").val();
	jianyanSelectEvent();// 检验经典模式select 数据填充
	addJyFenzuguize();// 检验报告分组规则添加
	addJianchaFenzu();//检查报告分组规则添加
	
	getAllParams();
	$.ajax({url:rootUrl+"/clidoctor/getBce03",type:"get",dataType:"json",data:{"bce01":bce01},async:false,success:function(data){bce03 = data.map.bce03}});
	reloadPatientInfo();
	autoExpand();
	/*页面上select2 控件初始化,改为一个一个初始化，医嘱类型初始化*/
	$("#bdaSearch").select2({
		language: "zh-CN"
	});
	$(".checkbox-inline").find("input").iCheck({
	    checkboxClass: 'icheckbox_square-blue',
	    radioClass: 'iradio_square-blue',
	    increaseArea: '20%' // optional
	});
	//医嘱附加信息的默认展开或者隐藏
	yizhuOtherInfo();
	
});

function getTitleText(list){
	var text = "门诊 "+list.vaa05+" 就诊信息";
	$(".title-text").text(text);
}
/**
 * 加载诊断信息下拉数据
 */
//获取界面参数设置
function getAllParams() {
	debugger;
	$.ajax({
		url : rootUrl + "/patientsearch/findSysparams",
		data : {userid:$.cookie("userId"),bck01:0},
		async : false,
		success : function(data) {
			var list = data.list;
			for (var i = 0; i < list.length; i++) {
				valueArr[list[i]["para_key"]] = list[i]["value"];
			}
			var jiezhenfw = valueArr.p_100_105002_100;
			if(jiezhenfw=="05" || jiezhenfw=="05")
			{
				$("#varjiezhen_fanwei").html("全院病人");
			}
		}
	});
	//加载新开页面的诊断树形结构数据
	$.ajax({url:rootUrl+"/treatment/jibingList",data:{},dataType:'json',type:'get', success:function(data){if(data)
	{
		jibingList = data.list;
	}}});
}
// 医嘱单全选事件
function choseAllEvent() {
	var all = $("#advice_table table thead").find("input[type='checkbox']");
	var sub = $("#advice_table table tbody").find(".innerHead").find("input[type='checkbox']");
	$(all).on('ifChecked', function(event){
		$(sub).iCheck('check');
	});
	$(all).on('ifUnchecked', function(event){
		$(sub).iCheck('uncheck');
	});
}


function reloadPatientInfo()
{
	debugger;
	var  lvac01 = $("#vac01").val();
	$.ajax({
		url : rootUrl + "/patientsearch/getpatientinfo",
		data : {
			"productid" : 100,
			"programid" :103010,
			"paramno" : 29,
			"lVAC01" : lvac01
		},
		type : 'post',
		dataType : 'json',
		async:false,
		success : function(data) {
			if(data){
				var list = $.extend(data.list[0],{status:0},{pinquiry:pinquiry});
				$("#vaa01").val(data.list[0]['vaa01']); //重要
			    laytplUrl_T(myurl + "/module/patient_more_info.html",list, function(data, html) {
			    	 $(".header-content-left").empty().append(html);
				});
			  //标题
				getTitleText(list);
				// 检查报告
			    jianChaJqGride();
			    jingDianJqGride();// 经典模式-左边列表
			    jianyueJqGride(); // 简约模式
			    
				patientDoctorAdvice(lvac01, 1);// 1
				// 病历记录
				queryApplyFileByPatientId(data.list[0]['vaa01'],data.list[0]['vaa05'], lvac01);
			}
		}
	});
}

function seeMoreInfoEvent(){
		var flag = $(".three-column").find(".main-header").hasClass('br_expanded');
		if(flag){
			$(".three-column").find(".main-header").removeClass('br_expanded');
			$(".three-column").find(".main-header").css('height','35px');
			$(".three-column").find('.more-info-text').text('更多');
			$(".three-column").find(".btn-transparent").find('.caret').removeClass('caret-top');
			$('.main-content').css('top','35px');
			/*$(".patientInfo_more").addClass("hidden");*/
			/*$(".header-content-left ul li").removeAttr('style');*/
			/*$(".header-content-left ul li").css('margin-right','10px');*/
			
		}else{
			$(".three-column").find(".main-header").addClass('br_expanded');
			$(".three-column").find(".main-header").css('height','110px');
			$(".three-column").find('.more-info-text').text('收起');
			$(".three-column").find(".btn-transparent").find('.caret').addClass('caret-top');
			$('.main-content').css('top','115px');
			/*$(".patientInfo_more").removeClass("hidden");*/
			/*$(".header-content-left ul li").css('width','auto');*/
			/*$(".header-content-left ul li").css('margin-right','10px');*/
		}
}
function yizhuOtherInfo(){
	var flag = $("#patient_operation").hasClass("expand");
	if(flag){
		$('.yizhu_content_table').animate({'height':"92%"},"fast");
		$("#patient_operation").removeClass("expand");
		$("#patient_operation").find('.more-info-text').text('展开');
		$("#yizhujilu_content").hide();
		$("#patient_operation").find('.caret').addClass('caret-top');
		
	}else{
		$('.yizhu_content_table').animate({'height':"55%"},"fast");
		$("#patient_operation").find('.more-info-text').text('收缩');
		$("#patient_operation").addClass("expand");
		$("#yizhujilu_content").show();
		$("#patient_operation").find('.caret').removeClass('caret-top');
	}
}

function autoExpand(){
	$("#patient_operation li").on("click",function(){
	if(!($(this).hasClass("moreCont"))){
		$('.yizhu_content_table').animate({'height':"55%"},"fast");
		$("#patient_operation").find('.more-info-text').text('收缩');
		$("#patient_operation").addClass("expand");
		$("#yizhujilu_content").show();
		$("#patient_operation").find('.caret').removeClass('caret-top');
	}
	})
}
// table折叠
function zhedie() {
	$(".innerHead").find("button").on("click",function() {
				var e=arguments.callee.caller.arguments[0]||event; 
				if (e && e.stopPropagation) { 
					// this code is for Mozilla and Opera
					e.stopPropagation(); 
				} else if (window.event) { 
					// this code is for IE 
					window.event.cancelBubble = true; 
				} 
				if ($(this).parents("tr").siblings().css("display") == "none") {
					$(this).parents("tr").find("lable").removeClass(
							"glyphicon glyphicon-chevron-up")
					$(this).parents("tr").find("lable").addClass(
							"glyphicon glyphicon-chevron-down");
					$(this).parents("tr").siblings().removeClass("hidden");
				} else {
					$(this).parents("tr").find("lable").removeClass(
							"glyphicon glyphicon-chevron-down");
					$(this).parents("tr").find("lable").addClass(
							"glyphicon glyphicon-chevron-up");
					$(this).parents("tr").siblings().addClass("hidden");
				}
				$(this).parents("tr").find("input[type='checkbox']").iCheck('uncheck');
			})
}


// 根据bck01加载医师列表
function loadBce1() {
	var bck01 = $("#zhuanrukeshi").find("option:selected").val();
	var url = rootUrl + "/treatment/loadBce1";
	var params = {};
	params.bck01 = bck01;
	$("#jiezhenyishi").empty();
	$.get(url, params, function(data) {
		var list = data.list;
		for (var i = 0; i < list.length; i++) {
			var option = $("<option>").attr('value', list[i]['bce01']).text(
					list[i]['bce03']);
			$("#jiezhenyishi").append(option);
		}
	});
}
// 医嘱单tr选中事件
function adviceChoseTr() {
	$("#advice_table").find(".yizhu_details").on(
			"click",
			function() {
				$(this).addClass("choseedTr").siblings().removeClass("choseedTr");
				$(this).parents("tbody").siblings().find("tr").removeClass("choseedTr");
			});
}
// 病人医嘱记录
function patientDoctorAdvice(lvac01) {
	var that = this;
	$.ajax({
		url : rootUrl + "/doctoradvice/getpatientsdoctoradvice",
		type : "post",
		async : false,
		data : {
			"lVAF06" : lvac01,
			"lVAF04" : 1,
			"lBCE01A" : bce01,
			 VAF74 : "",
			 VAF36 : ""
		},
		dataType : 'json',
		success : function(data) {
			that.laytplUrl_T(myurl + "/module/advice_table.html", data,function(data, html) {
						$("#advice_table").html(html);
						choseAllEvent();// 全选事件
						otherProperties();
						checkboxEvent1();
						checkboxEvent4();
						checkboxEvent8();
						advicetongzu();// 同组记录
						adviceChoseTr();// 医嘱tr选中事件
						backAdviceStyle();//医嘱作废样式
						zhedie();
						//icheck初始化
						$("#advice_table").find('input').iCheck({
						    checkboxClass: 'icheckbox_square-blue',
						    radioClass: 'iradio_square-blue',
						    increaseArea: '20%' // optional
						  });
					});
			
		}
	});
}
//作废样式
function backAdviceStyle(){
	$(".yizhu_details").each(function(index, ele) {
		var fstatus=JSON.parse($(ele).attr("data-info")).fstatus;
		if(fstatus=="医嘱退费"){
			$(this).addClass("backAdvice");
		}
	})
}
// 医嘱单同组
function advicetongzu() {
	var tzTdindex = [ 3, 6 ];
	$("#advice_table tbody").each(function(index, ele) {
						var vaf59Arr = [];
						var adviceList = $(ele).find(".yizhu_details");
						for (var k = 0; k < adviceList.length; k++) {
							var dataobj = JSON.parse($(adviceList[k]).attr("data-info"));
							vaf59Arr.push(dataobj.vaf59);
						}
						vaf59Arr = vaf59Arr.unique2();
						var resultArr = [];
						for (var i = 0; i < vaf59Arr.length; i++) {
							var tongzuEditArr = [];
							for (var j = 0; j < adviceList.length; j++) {
								if (JSON.parse($(adviceList[j]).attr("data-info")).vaf59 == vaf59Arr[i]) {
									tongzuEditArr.push($(adviceList[j]).index());
								}
							}
							if (tongzuEditArr.length > 1) {

								for (var m = 0; m < tzTdindex.length; m++) {// 要同组的tr
																			// 的下标
									for (var n = 0; n < tongzuEditArr.length; n++) { // 同组的td
																						// 的下标[3,6]
										if (n == 0) { // 同组tr为第一行的，只把td的bottom
														// 去掉
											$($(ele).find("tr")[tongzuEditArr[n]]).find("td:eq("+ tzTdindex[m]+ ")").addClass("xiyaoTzCss");
										} else if (n == (tongzuEditArr.length - 1)) {// 如果为最后一行，只把text隐藏
											$($(ele).find("tr")[tongzuEditArr[n]]).find("td:eq("+ tzTdindex[m]+ ")").text("");
										} else {// 如果为中间的tr，则隐藏text,并bottom 去掉
											$($(ele).find("tr")[tongzuEditArr[n]]).find("td:eq("+ tzTdindex[m]+ ")").text("");
											$($(ele).find("tr")[tongzuEditArr[n]]).find("td:eq("+ tzTdindex[m]+ ")").addClass("xiyaoTzCss");
										}
									}
								}
							}
						}

					})
}

// 检验报告select 下拉加载
function jianyanSelectEvent() {
	var data = [ {
		"name" : "生化",
		"value" : "sh"
	}, {
		"name" : "五分类检验报告",
		"value" : "wfljybg"
	}, {
		"name" : "统计",
		"value" : "tj"
	}, {
		"name" : "检验申请单",
		"value" : "jysqd"
	}, {
		"name" : "样本条码",
		"value" : "ybtm"
	}, {
		"name" : "样本检验A4",
		"value" : "ybjyA4"
	}, {
		"name" : "性激素检验报告（实验室）",
		"value" : "xjs_sys"
	}, {
		"name" : "微量元素检验报告",
		"value" : "wlys"
	}, {
		"name" : "酶标仪",
		"value" : "mby"
	}, {
		"name" : "实验室报告单",
		"value" : "sysbgd"
	}, {
		"name" : "性激素检验报告（免疫）",
		"value" : "xjs_my"
	}, {
		"name" : "酶标测试报告",
		"value" : "mbcsbg"
	}, {
		"name" : "样本条码",
		"value" : "ybtm"
	}, {
		"name" : "质控图",
		"value" : "zkt"
	}, {
		"name" : "血液查询",
		"value" : "xycx"
	}, {
		"name" : "血液_病人登记",
		"value" : "xy_brdj"
	}, {
		"name" : "血液_血液鉴定",
		"value" : "xy_xyjd"
	}, {
		"name" : "血库血液出库单",
		"value" : "xk_ckd"
	}, {
		"name" : "血库血液入库单）",
		"value" : "xk_rkd"
	}, {
		"name" : "血库血液报废单",
		"value" : "xk_bfd"
	}, {
		"name" : "血库血液退库单",
		"value" : "xk_tkd"
	}, {
		"name" : "手工报告单",
		"value" : "sgbgd"
	}, {
		"name" : "微量元素",
		"value" : "wlys"
	} ]

	for (var i = 0; i < data.length; i++) {
		var html = "<option>";
		html += data[i].name;
		html += "</option>";
		$("#jiangyan_baogaodan").append(html);
		$("#jiangyan_baogaodan").select2();
	}
}
//检查报告select 分组加载
function addJianchaFenzu(){
	var options = [ {"text" : "姓名","id" : "PAA07"}, {"text" : "检查仪器","id" : "BHF03"}, {"text" : "影像提示","id" : "PAB11"},
	                {"text" : "影像号","id" : "PAA06"}, {"text" : "阳性提示","id" : "PAA46"}, {"text" : "业务范围","id" : "ACF02"},
	                {"text" : "审核时间","id" : "PAB13"}];
	            $('#jiancha_fenzu1').select2({language: "zh-CN",data:options,placeholder:'请选择分组规则',allowClear: true});
	            $('#jiancha_fenzu2').select2({language: "zh-CN",data:options,placeholder:'请选择分组规则',allowClear: true});
	            $('#jiancha_fenzu3').select2({language: "zh-CN",data:options,placeholder:'请选择分组规则',allowClear: true});

	            $('#jiancha_fenzu1').val('').trigger('change');
	            $('#jiancha_fenzu2').val('').trigger('change');
	            $('#jiancha_fenzu3').val('').trigger('change');

	            $('#jiancha_fenzu1').bind('change',function(){
	            	jianChaJqGride();
	            });
	            $('#jiancha_fenzu2').bind('change',function(){
	            	jianChaJqGride();
	            });
	            $('#jiancha_fenzu3').bind('change',function(){
	            	jianChaJqGride();
	            });
}

function addJyFenzuguize() {
	var options = [ {"text" : "检验时间","id" : "FLAB20"}, {"text" : "仪器","id" : "BEY03"}, {"text" : "项目名称","id" : "BBX05"},
	                {"text" : "缩写","id" : "BFC03"}, {"text" : "结果","id" : "LAC08"}, {"text" : "单位","id" : "BDG02"},
	                {"text" : "状态","id" : "LAC11"}, {"text" : "参考范围","id" : "LAC15"}, {"text" : "打印","id" : "FPRINT"},
	                {"text" : "样本类型","id" : "ABB02"}, {"text" : "婴儿姓名","id" : "VAP05"}];
	            $('#jianyan_fenzu1').select2({language: "zh-CN",data:options,placeholder:'请选择分组规则',allowClear: true});
	            $('#jianyan_fenzu2').select2({language: "zh-CN",data:options,placeholder:'请选择分组规则',allowClear: true});
	            $('#jianyan_fenzu3').select2({language: "zh-CN",data:options,placeholder:'请选择分组规则',allowClear: true});

	            $('#jianyan_fenzu1').val('FLAB20').trigger('change');
	            $('#jianyan_fenzu2').val('BEY03').trigger('change');
	            $('#jianyan_fenzu3').val('').trigger('change');

	            $('#jianyan_fenzu1').bind('change',function(){
	            	jianyueJqGride();
	            });
	            $('#jianyan_fenzu2').bind('change',function(){
	            	jianyueJqGride();
	            });
	            $('#jianyan_fenzu3').bind('change',function(){
	            	jianyueJqGride();
	            });
	/*---------------------------经典模式----------------------------*/
	 var options = [ {"text" : "仪器","id" : "BEY03A"}, {"text" : "病区","id" : "BCK01D"}, {"text" : "工作单位","id" : "BAQ03"},
	                 {"text" : "病历号","id" : "VAA01"}, {"text" : "病人科室","id" : "BCK01A"}, {"text" : "病人类别","id" : "BDP02"},
	                 {"text" : "床号","id" : "LAB16"}, {"text" : "生理特征","id" : "BES02"}, {"text" : "标本形态","id" : "LAB42"},
	                 {"text" : "诊断","id" : "LAB18"}, {"text" : "执行科室","id" : "BCK03C"}, {"text" : "样本条码","id" : ""	},
	                 {"text" : "申请科室","id" : "BCK03B"} , {"text" : "审核时间","id" : "LAB53"}, {"text" : "检验人","id" : "BCE03D"},
	                 {"text" : "核收人","id" : "BCE03C"}, {"text" : "采样人","id" : "BCE03A"}];
	             $('#jd_fenzu1').select2({language: "zh-CN",data:options,placeholder:'请选择分组规则',allowClear: true});
	             $('#jd_fenzu2').select2({language: "zh-CN",data:options,placeholder:'请选择分组规则',allowClear: true});
	             $('#jd_fenzu3').select2({language: "zh-CN",data:options,placeholder:'请选择分组规则',allowClear: true});

	             $('#jd_fenzu1').val('BEY03A').trigger('change');
	             $('#jd_fenzu2').val('').trigger('change');
	             $('#jd_fenzu3').val('').trigger('change');

	             $('#jd_fenzu1').bind('change',function(){
	            	 jingDianJqGride();
	             });
	             $('#jd_fenzu2').bind('change',function(){
	            	 jingDianJqGride();
	             });
	             $('#jd_fenzu3').bind('change',function(){
	            	 jingDianJqGride();
	             });
}

// 根据医嘱类型查询医嘱记录
function changeAdcive() {
	var searchStr = $("#bdaSearch>option:selected").attr("id");
	if (searchStr != "") {
		$("#advice_table tbody").hide().filter(":contains('" + searchStr + "')").show();
	} else {
		$("#advice_table tbody").show();
	}
}

// 根据医嘱状态查询医嘱（前端实现）
function checkboxEvent1()
{
	$('#vaf10_1').on('ifChecked', function(event)
	{
		$("#advice_table tbody tr").find("td:eq(2)").filter(":contains('新开')").parents("tbody").show();
	});
	$('#vaf10_1').on('ifUnchecked', function(event)
	{
		$("#advice_table tbody tr").find("td:eq(2)").filter(":contains('新开')").parents("tbody").hide();
	});

}
// 发送
function checkboxEvent8()
{
	$('#vaf10_8').on('ifChecked', function(event)
	{
		$("#advice_table tbody tr").find("td:eq(2)").filter(":contains('已发送')").parents("tbody").show();
	});
	$('#vaf10_8').on('ifUnchecked', function(event)
	{
		$("#advice_table tbody tr").find("td:eq(2)").filter(":contains('已发送')").parents("tbody").hide();
	});

}

function checkboxEvent4()
{
	// 作废
	$('#vaf10_4').on('ifChecked', function(event)
	{
		$("#advice_table tbody tr").find("td:eq(2)").filter(":contains('医嘱退费')").parents("tbody").show();
	});
	$('#vaf10_4').on('ifUnchecked', function(event)
	{
		$("#advice_table tbody tr").find("td:eq(2)").filter(":contains('医嘱退费')").parents("tbody").hide();
	});
}

// 医嘱记录更多操作
function moreOperation() {
	$("#patient_operation .moreCont").on("click", function() {
		if ($("#yizhujilu_content").css("display") == "none") {
			$("#table_list").addClass("hidden");
			$("#yizhujilu_content").removeClass("hidden");
			$("#patient_operation .moreCont").text("折叠");
		} else {
			$("#table_list").removeClass("hidden");
			$("#yizhujilu_content").addClass("hidden");
			$("#patient_operation .moreCont").text("展开");
		}

	})
}
 
function headPrice(cbm01)
{
	$.post(rootUrl+"/doctoradvice/doctoradvicedetail",{"lCBM01":cbm01},function(data)
	{
		vbd_jqgrid(data.list);
	});
}


// 医嘱单选定，并查看医嘱信息
function otherProperties()
{
	loadZhenjinEditTable();
	$(".innerHead").on("click",function()
	{
		if($(this).find("input:checked").length>0){
			$(this).find("input[type='checkbox']").iCheck('uncheck');
		}else{
			
			$(this).find("input[type='checkbox']").iCheck('check');
		}
		var cbm01 = $(this).attr("data-cbm01list");
		var data_info = $($(this).parents("tbody").find(".yizhu_details")[0]).attr("data-info");
		var vaf10 = JSON.parse(data_info).vaf10;
		
		var vaf01 = JSON.parse(data_info).vaf01;
		debugger;
		if(vaf10==1)
		{
			//1349
			$.post(rootUrl+"/doctoradvice/doctoradvicepriceinfo",{"lCBM01":cbm01,"ABC02":$("abc02").text()},function(data)
					{
						$("#vaf1Fasongjilu_table").empty();
						vbd_jqgrid(data.list);
					});
		}
		else if(vaf10==8)
		{
			//1402
			dataObjT={fun:'1',cbm01:cbm01};
			headPrice(cbm01);
			//1405 医嘱单发送记录
			$.post(rootUrl+"/doctoradvice/sendadvicehistory",{"lCBM01":cbm01},function(data)
					{
				var list = data.list;
				$("#vaf1Fasongjilu_table").empty();
	    		for(var i= 0;i<list.length;i++)
	    		{
	    			var td1 = $("<td>"+(list[i]["fvbi15"]?list[i]["fvbi15"]:'未结账')+"</td>");
	    			var td2 = $("<td>"+(list[i]["vbi09"]?list[i]["vbi09"]:'')+"</td>");
	    			var td3 = $("<td>"+(list[i]["nbby05"]?list[i]["nbby05"]:'')+"</td>");
	    			var td5 = $("<td>"+(list[i]["vbi07"]?list[i]["vbi07"]:'')+"</td>");
	    			var td6 = $("<td>"+(list[i]["bck03"]?list[i]["bck03"]:'')+"</td>");
	    			var td7 = $("<td>"+(list[i]["bce03a"]?list[i]["bce03a"]:'')+"</td>");
	    			var td8 = $("<td>"+(list[i]["fvbi13"]?list[i]["fvbi13"]:'')+"</td>");
	    			var td9 = $("<td>"+(list[i]["vai04"]?list[i]["vai04"]:'')+"</td>");
	    			var td10 = $("<td>"+(list[i]["vbi21"]?list[i]["vbi21"]:'')+"</td>");
	    			var tr = $("<tr></tr>");
	    			tr.append(td1);
	    			tr.append(td2);
	    			tr.append(td3);
//	    			tr.append(td5);
	    			tr.append(td6);
	    			tr.append(td7);
	    			tr.append(td8);
	    			tr.append(td9);
	    			tr.append(td10);
	    			$("#vaf1Fasongjilu_table").append(tr);
	    		}
					});
		}
		//7501 医嘱单处方属性
		var params = {};
		params.iaa01 = 0;
		params.acf01 = 1;
		
		params.vaf01 = vaf01;
	    	$.post(rootUrl+"/doctoradvice/doctoradviceprescriptionproperty",params,function(data){
	    		var list = data.list;
	    		$("#chufangshuxing_table tbody").empty();
	    		for(var i= 0;i<list.length;i++)
	    		{
	    			var td1 = $("<td>"+(list[i]["cbm07"]?list[i]["cbm07"]:'')+"</td>");
	    			var td2 = $("<td>"+(list[i]["bjw02"]?list[i]["bjw02"]:'')+"</td>");
	    			var td3 = $("<td>"+(list[i]["iai03"]?list[i]["iai03"]:'')+"</td>");
	    			var tr = $("<tr></tr>");
	    			tr.append(td1);
	    			tr.append(td2);
	    			tr.append(td3);
	    			$("#chufangshuxing_table tbody").append(tr);
	    		}
	    	});
		//1176 医嘱单处方诊断//暂时注释掉，新的在zhenduan.js
	    	adviceDiagnose(cbm01);
	});
	$(".yizhu_details").on("click", function(e)
	{
		/*
		 * if(e.target.tagName=="INPUT"){ return; }
		 */
		$(this).addClass("choseedTr").parent().siblings().find(".yizhu_details").removeClass("choseedTr");
		
		var dataStr = $(this).attr("data-info");
		var dataObj = JSON.parse(dataStr);
		cbm01 = dataObj.cbm01;
		dataObjT={fun:'2',dataObj:dataObj};
		vbdInfo(dataObj);
		// 处方诊断
		adviceDiagnose(dataObj.cbm01);
		// 处方性质
		chufangshuxing(dataObj.vaf01);
		// 手术人员安排
		shouShuRenYuanAnpai(dataObj.vaf01);
		//1405 医嘱单发送记录
		$.post(rootUrl+"/doctoradvice/sendadvicehistory_vaf",{"vaf01":dataObj.vaf01,"cbm01":cbm01},function(data)
				{
			
			var list = data.list;
			$("#vaf1Fasongjilu_table").empty();
    		for(var i= 0;i<list.length;i++)
    		{
    			var td1 = $("<td>"+(list[i]["fvbi15"]?list[i]["fvbi15"]:'未结账')+"</td>");
    			var td2 = $("<td>"+(list[i]["vbi09"]?list[i]["vbi09"]:'')+"</td>");
    			var td3 = $("<td>"+(list[i]["nbby05"]?list[i]["nbby05"]:'')+"</td>");
    			var td5 = $("<td>"+(list[i]["vbi07"]?list[i]["vbi07"]:'')+"</td>");
    			var td6 = $("<td>"+(list[i]["bck03"]?list[i]["bck03"]:'')+"</td>");
    			var td7 = $("<td>"+(list[i]["bce03a"]?list[i]["bce03a"]:'')+"</td>");
    			var td8 = $("<td>"+(list[i]["fvbi13"]?list[i]["fvbi13"]:'')+"</td>");
    			var td9 = $("<td>"+(list[i]["vai04"]?list[i]["vai04"]:'')+"</td>");
    			var td10 = $("<td>"+(list[i]["vbi21"]?list[i]["vbi21"]:'')+"</td>");
    			var tr = $("<tr></tr>");
    			tr.append(td1);
    			tr.append(td2);
    			tr.append(td3);
    			/*tr.append(td5);*/
    			tr.append(td6);
    			tr.append(td7);
    			tr.append(td8);
    			tr.append(td9);
    			tr.append(td10);
    			$("#vaf1Fasongjilu_table").append(tr);
    		}
				});
		// 加载打印医嘱列表

		// printAdvice(bda01);
	})

}
//选择打印格式
function chosePrintType(){
	 var e=arguments.callee.caller.arguments[0]||event; 
	 if (e && e.stopPropagation) { 
	  // this code is for Mozilla and Opera
	  e.stopPropagation(); 
	 } else if (window.event) { 
	  // this code is for IE 
	  window.event.cancelBubble = true; 
	 } 
}

// 医嘱打印预览
function print1() {
	debugger;
	var datastr = $("#advice_print option:selected").attr("data-info");
	if(datastr){
		var dataObj = JSON.parse(datastr);
		var id = dataObj.id;
		var vaf10 = dataObj.vaf10;
		if(vaf10 == '8'){
			var url = printUrl + "/printtoolrest/printToPdfView/" + id + "?cbm01="
			+ cbm01+"&jsessionids="+$.cookie('jsessionids');
			window.open(url);
		}else{
			if(vaf10 == '1'){
				layer.alert("请先发送医嘱单！");
			}else if(vaf10 == '4'){
				layer.alert("医嘱已作废，请重开！");
			}
		}
	}else{
		layer.msg("请选择打印的医嘱单！",{time:2000,icon:0,skin: 'layui-layer-msg0'});
	}
}
//打印选中医嘱单，添加打印类型
function printCheckedEvent(){
	if ($(".innerHead td input:checked").length <= 0)
	{
		layer.msg("请勾选要打印的医嘱单",
		{
		time : 2000,
		icon : 0,
		skin: 'layui-layer-msg0'
		});
		return;
	}
	else if($(".innerHead td input:checked").length > 1)
	{
		layer.msg("只能勾选一个已发送的医嘱单",
				{
				time : 2000,
				icon : 0,
				skin: 'layui-layer-msg0'
				});
				return;
	}
	else
	{
			var dataStr=$($(".innerHead td input:checked").parents("tbody").find(".yizhu_details")[0]).attr("data-info");
			var dataObj = JSON.parse(dataStr);
			 cbm01 = dataObj.cbm01;
			var bda01 = dataObj.bda01;
			var vaf10 = dataObj.vaf10;
			if(vaf10!=8)
			{
				layer.msg("请先发送医嘱单",
						{
						time : 2000,
						icon : 0,
	    				skin: 'layui-layer-msg0'
						});
						return;
			}
			printAdvice(bda01,vaf10,dataObj.cbm07);//vaf10 判断是否是医嘱单状态
		}
}

//加载打印格式下拉列表
function printAdvice(bda01,vaf10,cbm07) {
	var rid;
	switch (bda01)
	{
		case "1" :
			rid = justXiyaoType(cbm07,rid);
			break;
		case "2" :
			rid = justXiyaoType(cbm07,rid);
			break;
		case "8" :
			rid = 25;
			break;
		case "E" :
			rid = 22;
			break;
		case "L" :
			rid = 23;
			break;
		case "S" :
			rid = 28;
			break;
		case "4" :
			rid = 999;//待定
			break;
		case "M" :
			rid = 999;//待定
			break;
		default ://治疗
			rid = 190;
			break;
	}
	var html ="";
	var myAjax = $.ajax({
		url : printUrl + "/printtemprest/findListByRid/" + rid+"?jsessionids="+$.cookie('jsessionids'),
		type : "get",
		async : true,
		dataType : "json",
		traditional : true,
		success : function(data) {
			var defaultV1;
			for (var i = 0; i < data.length; i++) {
				var obj = {};
				obj.vaf10 = vaf10;
				obj.id = data[i]['id'];
				if (data[i]['xdefault'] == "1") {
					defaultV1 = data[i].id;
					 html += '<option data-info='+JSON.stringify(obj)+' value='+data[i].id+'>'+data[i]['name']+'</option>';
				} else {
					 html += '<option data-info='+JSON.stringify(obj)+' value='+data[i].id+'>'+data[i]['name']+'</option>';
				}
			}
			$("#advice_print").empty().append(html);
			$("#advice_print").select2();
			if(defaultV1){
				
				$("#advice_print").val(defaultV1).trigger("change");
			}
			if (bda01=="1")
			{
				$(".iswest").show();
				var html1 = "";
				//输液卡
				$.ajax(
				{
				url : printUrl + "/printtemprest/findListByRid/" + 101 + "?jsessionids=" + $.cookie('jsessionids'),
				type : "get",
				async : false,
				dataType : "json",
				traditional : true,
				success : function(data)
				{
					var defaultV2;
					for (var i = 0; i < data.length; i++)
					{
						var obj = {};
						obj.vaf10 = vaf10;
						obj.id = data[i]['id'];
						if (data[i]['xdefault'] == "1")
						{
							defaultV2 = data[i].id;
							html1 += '<option data-info=' + JSON.stringify(obj) + ' value='+data[i].id+'>' + data[i]['name'] + '</option>';
						}
						else
						{
							html1 += '<option data-info=' + JSON.stringify(obj) + ' value='+data[i].id+'>' + data[i]['name'] + '</option>';
						}
					}
					$("#advice_print_shuye").empty().append(html1);
					$("#advice_print_shuye").select2();
					if(defaultV2){
						
						$("#advice_print_shuye").val(defaultV2).trigger("change");
					}
				}
				});
				//皮试单
				var html2="";
				$.ajax(
						{
							url : printUrl + "/printtemprest/findListByRid/" + 102 + "?jsessionids=" + $.cookie('jsessionids'),
							type : "get",
							async : false,
							dataType : "json",
							traditional : true,
							success : function(data)
							{
								var defaultV3;
								for (var i = 0; i < data.length; i++)
								{
									var obj = {};
									obj.vaf10 = vaf10;
									obj.id = data[i]['id'];
									if (data[i]['xdefault'] == "1")
									{
										defaultV3 = data[i].id;
										html2 += '<option data-info=' + JSON.stringify(obj) + '  value='+data[i].id+'>' + data[i]['name'] + '</option>';
									}
									else
									{
										html2 += '<option data-info=' + JSON.stringify(obj) + '  value='+data[i].id+'>' + data[i]['name'] + '</option>';
									}
								}
								$("#advice_print_pishi").empty().append(html2);
								$("#advice_print_pishi").select2();
								if(defaultV3){
									
									$("#advice_print_pishi").val(defaultV3).trigger("change");
								}
							}
						});
				
			}
			else
			{
				$("#advice_print_shuye").empty();
				$("#advice_print_pishi").empty();
				$(".iswest").hide();
			}
			
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			layer.msg('打印服务器连接失败！', {
				time : 2000,
				icon : 3,
				skin: 'layui-layer-msg3'
			});
		}
	});

}
function print11() {
	var datastr = $("#advice_print_shuye option:selected").attr("data-info");
	if(datastr){
		var dataObj = JSON.parse(datastr);
		var id = dataObj.id;
		var vaf10 = dataObj.vaf10;
		if(vaf10 == '8'){
			var url = printUrl + "/printtoolrest/printToPdfView/" + id + "?cbm01="
			+ cbm01+"&jsessionids="+$.cookie('jsessionids');
			window.open(url);
		}else{
			if(vaf10 == '1'){
				layer.alert("请先发送医嘱单！");
			}else if(vaf10 == '4'){
				layer.alert("医嘱已作废，请重开！");
			}
		}
	}else{
		layer.msg("请选择打印的医嘱单！",{time:2000,icon:0,skin: 'layui-layer-msg0'});
	}
}
function print111() {
	var datastr = $("#advice_print_pishi option:selected").attr("data-info");
	if(datastr){
		var dataObj = JSON.parse(datastr);
		var id = dataObj.id;
		var vaf10 = dataObj.vaf10;
		if(vaf10 == '8'){
			var url = printUrl + "/printtoolrest/printToPdfView/" + id + "?cbm01="
			+ cbm01+"&jsessionids="+$.cookie('jsessionids');
			window.open(url);
		}else{
			if(vaf10 == '1'){
				layer.alert("请先发送医嘱单！");
			}else if(vaf10 == '4'){
				layer.alert("医嘱已作废，请重开！");
			}
		}
	}else{
		layer.msg("请选择打印的医嘱单！",{time:2000,icon:0,skin: 'layui-layer-msg0'});
	}
}


// 加载病人的处方诊断信息
function chufangzhenduan(vaf01) {
	var url = rootUrl + "/doctoradvice/doctordiagnosis";
	var params = {};
	params.lVAF01 = vaf01;
	params.acf01 = 1;
	$.post(url, params, function(data) {
		var list = data.list;
		$("#chufangzhenduan_table").empty();
		for (var i = 0; i < list.length; i++) {
			var tr = $("<tr></tr>");
			var td2 = $("<td></td>");
			var td22 = $("<td>" + (list[i]["vao15"]?list[i]["vao15"]:'') + "</td>");
			tr.append(td2);
			tr.append(td22);
			$("#chufangzhenduan_table").append(tr);
		}
//		tr.append(td1);
	})
}
    
    
    
    //加载病人处方属性
    function chufangshuxing(vaf01)
    {
    	var url = rootUrl + "/doctoradvice/doctoradviceprescriptionproperty";;
    	var params= {};
    	params.iaa01 = 0;
    	params.acf01 = 1;
    	params.vaf01 = vaf01;
    	$.post(url,params,function(data){
    		var list = data.list;
    		
    		$("#chufangshuxing_table tbody").empty();
    		for(var i= 0;i<list.length;i++)
    		{
    			var td1 = $("<td>"+(list[i]["cbm07"]?list[i]["cbm07"]:'')+"</td>");
    			var td2 = $("<td>"+(list[i]["bjw02"]?list[i]["bjw02"]:'')+"</td>");
    			var td3 = $("<td>"+(list[i]["iai03"]?list[i]["iai03"]:'')+"</td>");
    			var tr = $("<tr></tr>");
    			tr.append(td1);
    			tr.append(td2);
    			tr.append(td3);
    			$("#chufangshuxing_table tbody").append(tr);
    		}
    	});
    }
    
    //加载手术人员安排
    function shouShuRenYuanAnpai(vaf01)
    {
    	var url = rootUrl + "/doctoradvice/queryoperationscheduled";;
    	var params= {};
    	params.VAF01 = vaf01;
    	$.post(url,params,function(data){
    		var list = data.list;
    		$("#shoushurenyuananpai_table tbody").empty();
    		for(var i= 0;i<list.length;i++)
    		{
    			var td1 = $("<td>"+list[i]["ace02"]+"</td>");
    			var td2 = $("<td>"+list[i]["bce02"]+"</td>");
    			var td3 = $("<td>"+list[i]["bce03"]+"</td>");
    			var tr = $("<tr></tr>");
    			tr.append(td1);
    			tr.append(td2);
    			tr.append(td3);
    			$("#shoushurenyuananpai_table").append(tr);
    		}
    	});
    }
    
    //医嘱发送记录(草药和手术)
    function vaf1Fasongjilu3(vaf01)
    {
    	var url = rootUrl + "/sendadvicehistory/herbandoperationsendhistory";;
    	var params= {};
    	params.lVAFID = vaf01;
    	$.post(url,params,function(data){
    		var list = data.list;
    		$("#vaf1Fasongjilu_table").empty();
    		for(var i= 0;i<list.length;i++)
    		{
    			var td1 = $("<td>"+(list[i]["fvbi15"]?list[i]["fvbi15"]:'未结账')+"</td>");
    			var td2 = $("<td>"+(list[i]["vbi09"]?list[i]["vbi09"]:'')+"</td>");
    			var td3 = $("<td>"+(list[i]["nbby05"]?list[i]["nbby05"]:'')+"</td>");
    			var td5 = $("<td>"+(list[i]["vbi07"]?list[i]["vbi07"]:'')+"</td>");
    			var td6 = $("<td>"+(list[i]["bck03"]?list[i]["bck03"]:'')+"</td>");
    			var td7 = $("<td>"+(list[i]["bce03a"]?list[i]["bce03a"]:'')+"</td>");
    			var td8 = $("<td>"+(list[i]["fvbi13"]?list[i]["fvbi13"]:'')+"</td>");
    			var td9 = $("<td>"+(list[i]["vbi05"]?list[i]["vbi05"]:'')+"</td>");
    			var tr = $("<tr></tr>");
    			tr.append(td1);
    			tr.append(td2);
    			tr.append(td3);
    			tr.append(td5);
    			tr.append(td6);
    			tr.append(td7);
    			tr.append(td8);
    			tr.append(td9);
    			$("#vaf1Fasongjilu_table").append(tr);
    		}
    	});
    }
    
    //医嘱发送记录(西药)
    function vaf1Fasongjilu1(vaf01)
    {
    	var url = rootUrl + "/sendadvicehistory/sendpharmacyhistory";;
    	var params= {};
    	params.lVAF01 = vaf01;
    	params.lVAF01A = vaf01;
    	$.post(url,params,function(data){
    		var list = data.list;
    		$("#vaf1Fasongjilu_table").empty();
    		for(var i= 0;i<list.length;i++)
    		{
    			var td1 = $("<td>"+(list[i]["fvbi15"]?list[i]["fvbi15"]:'')+"</td>");
    			var td2 = $("<td>"+(list[i]["vbi09"]?list[i]["vbi09"]:'')+"</td>");
    			var td3 = $("<td>"+(list[i]["nbby05"]?list[i]["nbby05"]:'')+"</td>");
    			var td5 = $("<td>"+(list[i]["vbi07"]?list[i]["vbi07"]:'')+"</td>");
    			var td6 = $("<td>"+(list[i]["bck03"]?list[i]["bck03"]:'')+"</td>");
    			var td7 = $("<td>"+(list[i]["bce03a"]?list[i]["bce03a"]:'')+"</td>");
    			var td8 = $("<td>"+(list[i]["fvbi13"]?list[i]["fvbi13"]:'')+"</td>");
    			var td9 = $("<td>"+(list[i]["vbi01"]?list[i]["vbi01"]:'')+"</td>");
    			var tr = $("<tr></tr>");
    			tr.append(td1);
    			tr.append(td2);
    			tr.append(td3);
    			tr.append(td5);
    			tr.append(td6);
    			tr.append(td7);
    			tr.append(td8);
    			tr.append(td9);
    			$("#vaf1Fasongjilu_table").append(tr);
    		}
    	});
    }
    
    
    //医嘱发送记录(其它)
    function vaf1FasongjiluOther(vaf01)
    {
    	var url = rootUrl + "/sendadvicehistory/otheradvicesendhistory";
    	var params= {};
    	params.VAF01 = vaf01;
    	$.post(url,params,function(data){
    		var list = data.list;
    		$("#vaf1Fasongjilu_table").empty();
    		for(var i= 0;i<list.length;i++)
    		{
    			var td1 = $("<td>"+(list[i]["fvbi15"]?list[i]["fvbi15"]:'')+"</td>");
    			var td2 = $("<td>"+(list[i]["vbi09"]?list[i]["vbi09"]:'')+"</td>");
    			var td3 = $("<td>"+(list[i]["nbby05"]?list[i]["nbby05"]:'')+"</td>");
    			var td5 = $("<td>"+(list[i]["vbi07"]?list[i]["vbi07"]:'')+"</td>");
    			var td6 = $("<td>"+(list[i]["bck03"]?list[i]["bck03"]:'')+"</td>");
    			var td7 = $("<td>"+(list[i]["bce03a"]?list[i]["bce03a"]:'')+"</td>");
    			var td8 = $("<td>"+(list[i]["fvbi13"]?list[i]["fvbi13"]:'')+"</td>");
    			var td9 = $("<td>"+(list[i]["vbi05"]?list[i]["vbi05"]:'')+"</td>");
    			var tr = $("<tr></tr>");
    			tr.append(td1);
    			tr.append(td2);
    			tr.append(td3);
    			tr.append(td5);
    			tr.append(td6);
    			tr.append(td7);
    			tr.append(td8);
    			tr.append(td9);
    			$("#vaf1Fasongjilu_table").append(tr);
    		}
    	});
    }
    
    
    
    //根据医嘱状态查询医嘱（前端实现）
    function checkboxEvent1()
	{
    	$('#vaf10_1').on('ifChecked', function(event){
    		$("#advice_table tbody tr").find("td:eq(2)").filter(":contains('新开')").parents("tbody").show(); 
  		});
    	$('#vaf10_1').on('ifUnchecked', function(event){
    		$("#advice_table tbody tr").find("td:eq(2)").filter(":contains('新开')").parents("tbody").hide();
  		});
		
	}
  //发送
    function checkboxEvent8()
    {
    	$('#vaf10_8').on('ifChecked', function(event){
    		$("#advice_table tbody tr").find("td:eq(2)").filter(":contains('已发送')").parents("tbody").show(); 
  		});
    	$('#vaf10_8').on('ifUnchecked', function(event){
    		$("#advice_table tbody tr").find("td:eq(2)").filter(":contains('已发送')").parents("tbody").hide();  
  		});
    	
    }
    
    function checkboxEvent4()
    {
    	//作废
    	$('#vaf10_4').on('ifChecked', function(event){
    		$("#advice_table tbody tr").find("td:eq(2)").filter(":contains('医嘱退费')").parents("tbody").show();  
  		});
    	$('#vaf10_4').on('ifUnchecked', function(event){
    		$("#advice_table tbody tr").find("td:eq(2)").filter(":contains('医嘱退费')").parents("tbody").hide();  
  		});
    }
    function loadZhenjinEditTable()
    {
    	//加载诊金编辑项目
    	//查询诊查费列表
//    	var zj_edit_row1;
//    	var zj_edit_col1;
    	var zhenjinList_app = [];
    	var sendzhenjinList_app = [];
    	var temp0 = {vac01:$("#vac01").val()};
    	$.when(
    			$.ajax({url:rootUrl+"/clidoctor/queryZjList",type:"get",dataType:"json",success:function(data1){
    				zhenjinList_app=data1.list;
    				}}),
    			$.get(rootUrl+"/clidoctor/querySendZjByvac01",{params:JSON.stringify(temp0)},function(data2){
    				sendzhenjinList_app=data2.list;
    				})
    		).done(
    				function()
    				{
    					if (sendzhenjinList_app && sendzhenjinList_app.length>0)
    					{
    					for(var i =0;i<zhenjinList_app.length;i++)
    					{
    						zhenjinList_app[i].vaj01 = sendzhenjinList_app[0].vaj01;
    					}
    					$('#zhenjin_edit_container').resize(function(){
    						$("#zhenjin_edit_table").setGridWidth($("#zhenjin_edit_container").width()*1);
    					});
    					$("#zhenjin_edit_table").GridUnload();
    					$("#zhenjin_edit_table").jqGrid(
    							{
    								data : zhenjinList_app,
    								datatype : "local",
    								height : 'auto',
//    								width : 'auto',
    								autowidth : true,
//    								forceFit:true,
//    							    shrinkToFit:true,
    								multiselect : true,
    								multiboxonly:true,
    								colModel : [
    								            {
    								            	label:'项目名称',
    								            	name : 'bby05',
    								            	align : 'left',
    								            	width:'400'
    								            },
    								            {
    								            	name : 'bdn01',
    								            	hidden : true
    								            },
    								            {
    								            	name : 'bby01',
    								            	hidden : true
    								            },
    								            {
    								            	name : 'vaj01',
    								            	hidden : true
    								            },
    								            {
    								            	label : '数量',
    								            	name : 'amount',
    								            	align : 'left',
    								            	formatter : function(value, options, rData){
    								       		    	if(value){
    								       		    		 val =value;
    								       		    	   }else{
    								       		    		 val=1;
    								       		    	   }
    								       		    	return val;
    								       		    	}
    								            },
    								            {
    								            	label : '单价',
    								            	name : 'bby25',
    								            	align : 'left'
    								            },
    								            {
    								            	label : '金额',
    								            	name : 'bby25',
    								            	align : 'left'
    								            }
    								            ],
//    											beforeEditCell : function(rowid, cellname, value, iRow, iCol)
//    											{
//    												zj_edit_row1 = iRow;
//    												zj_edit_col1 = iCol;
//    											},
    								            ondblClickRow : function(rowid, status)
    								            {
    								            	var row = $("#zhenjin_edit_table").jqGrid("getRowData", rowid);
    								            },
    								            loadComplete: function (data3) 
    								            { 
    								            	var rowIds = jQuery("#zhenjin_edit_table").jqGrid('getDataIDs');
    								    				for(var j=0;j<rowIds.length;j++){
    								    					var curRowData = jQuery("#zhenjin_edit_table").jqGrid('getRowData', rowIds[j]);
    								    					if(sendzhenjinList_app[0].bby01==curRowData['bby01']){
    								    						$('#zhenjin_edit_table').setSelection(rowIds[j]);
    								    					}
    								    			}

    								            }
    							})
    					}
    				}
    		);
    }
